package cn.mxz.generator;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import cn.mxz.generator.base.GeneratorImpl;
import cn.mxz.generator.config.GeneratorConfig;
import cn.mxz.generator.config.IBeanAble;
import cn.mxz.generator.config.IClass;
import cn.mxz.generator.config.IField;
import cn.mxz.generator.config.IInterface;
import cn.mxz.generator.config.IMethod;
import cn.mxz.generator.config.MethodBack;
import cn.mxz.generator.util.StringUtils;

public class JavaHandlerGenerator extends GeneratorImpl {

	
	public JavaHandlerGenerator(GeneratorConfig config) {
		super(config, "server_src.handler");
	}

	@Override
	public void generator() {
		
		String content = config.getTemplet("JavaHandler");
		content = content.replaceAll("PACKET_CASES", buildCases());
//		content = content.replaceAll("OTHER_RESPONSE_CASES", buildResponses());
		content = content.replaceAll("PACKAGE_NAME", getPackageName());
		
		String path = getSrcPath();
		config.getFileUtil(path + File.separator + "DataHandlerImpl.java").writeToFile(content);
	}

	private String buildCases() {
		List<IClass> all = config.getClazzs();
		StringBuilder cases = new StringBuilder();
		for (IClass c : all) {
			for (IMethod m : c.getMethods()) {
				cases.append(buildCase(c, m, config));
			}
		}
		return cases + "";
	}

//	/**
//	 * 构建某个操作完成后的返回
//	 * 
//	 * 比如强化装备后, 返回战斗力给客户端
//	 * 
//	 * (所有Case块)
//	 * 
//	 * @return
//	 */
//	private String buildResponses() {
//		List<IClass> all = config.getClazzs();
//
//		String content = config.getTemplet("JavaHandlerResponse");
//		
//		StringBuilder cases = new StringBuilder();
//		for (IClass c : all) {
//			for (IMethod m : c.getMethods()) {
//				String text = content.replaceAll("RESPONSES", buildResponseCase(m) + "\r");
//				text = text.replaceAll("METHOD_ID", m.getId() + "");
//				cases.append(text);
//			}
//		}
//		
//		return cases + "";
//		
//	}

	/**
	 * 构建单个方法的一个CASE, 这个CASE里面包含了多个返回
	 * @param m
	 * @return
	 */
	private String buildResponseCase(IMethod m) {
		StringBuilder cases = new StringBuilder();
		List<MethodBack> mbs = m.getBacks();
		for (MethodBack mb : mbs) {
			cases.append(buildResponse(mb) + "\r");
		}
		return cases + "";
	}

	private Object buildResponse(MethodBack mb) {
		
		String content = config.getTemplet("JavaHandlerResponseCase");
		
		IMethod m = mb.getMethod();
		IInterface c = mb.getClazz();
		
		content = buildCase(m, c, content);
		
		content = content.replace("FIELDS", buildFileds(mb.getArgs()));
		
		return content;
	}

	private CharSequence buildFileds(List<String> args) {
		Iterator<String> it = args.iterator();
		String s = "";
		while (it.hasNext()) {
			String arg = it.next();
			s += arg;
			if(it.hasNext()) {
				s += ",";
			}
		}
		
		return s;
	}

	private String buildCase(IMethod m, IBeanAble c, String content) {

		content = content.replace("METHOD_NAME", m.getName());
		content = content.replace("INTERFACE_NAME", c.getInterfaceName());
		content = content.replace("BEAN_NAME", c.getBeanName() + "Service");
		
		content = content.replaceAll("METHOD_ID", m.getId() + "");
		
		if(m.getReturn().isVoid()) {
			content = content.replaceAll("RETURN_TYPE data = ", "");
			content = content.replaceAll("PUT_DATA", "");
		} else {
			content = content.replaceAll("RETURN_TYPE", m.getReturn().getTypeSimple());
			if(m.getReturn().isBaseType()) {
				String put = StringUtils.firstToUpperCase(m.getReturn().getType());
				content = content.replaceAll("PUT_DATA", "response.put" + put + "(data);");
			} else {
				content = content.replaceAll("PUT_DATA", "response.put(data);");
			}
		}
		return content;
	}

	private String buildCase(IClass c, IMethod m, GeneratorConfig config) {
		String content = config.getTemplet("JavaHandlerCase");

		content = buildCase(m, c, content);
		
		content = content.replace("PACKET_NUMBER", m.getId() + "");
		content = content.replace("READ_FILEDS", buildReadFileds(m.getArgs()));
		content = content.replace("INSERT_FIELDS", buildFiledsLinkBy(", ", m.getArgs()));
		
		content = content.replace("RESPONSES", buildResponseCase(m));
		
		return content;
	}


	/**
	 * 将args中的所有成员的名字用sp分隔， 连成一个字符串
	 * @param sp	分隔符
	 * @param args
	 * @return
	 */
	private String buildFiledsLinkBy(String sp, List<IField> args) {
		StringBuilder sb = new StringBuilder();
		Iterator<IField> it = args.iterator();
		while (it.hasNext()) {
			IField f = it.next();
			sb.append(f.getName());
			if(it.hasNext()) {
				sb.append(sp);
			}
		}
		return sb.toString();
	}

	
	private String buildReadFileds(List<IField> args) {
		StringBuilder sb = new StringBuilder();
		for (IField f : args) {
			sb.append("\t\t\t" + f.getType() + " " + f.getName() + " = " + "in.get" + StringUtils.firstToUpperCase(f.getType()) + "();\r");
		}
		return sb.toString();
	}

}
